﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XMLGener.Data;
using XMLGener.interfaces;

namespace XMLGener.GUI.components
{
    /// <summary>
    /// This Paneln item is used for selecting one option from list.
    /// 
    /// </summary>
    public partial class XComboItem : UserControl,IXModuleField
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public XComboItem()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="param"></param>
        public void setAttr(XModuleParam param)
        {
            this.label1.Text = param.name;
            this.list = param.itemsList;
            foreach (XPairClass p in param.itemsList.Values)
            {
                comboBox1.Items.Add(p);
            }
            this.id = param.id;
        }

        private string id = null;
        private XMLGenerNode node = null;
        private Dictionary<string, XPairClass> list = null;

        #region IXModuleField Members

        /// <summary>
        /// Set source nodes
        /// </summary>
        /// <param name="node"></param>
        public void setXNode(XMLGenerNode node)
        {
            this.node = node;
            if (node != null)
            {
                string selectedItem = node.getSetting(id);
                if (!string.IsNullOrEmpty(selectedItem) && list.ContainsKey(selectedItem))
                    this.comboBox1.SelectedItem = list[selectedItem];
                else
                    this.comboBox1.SelectedIndex = -1;
            }
        }


        #endregion

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XPairClass selected = comboBox1.SelectedItem as XPairClass;
            if (node != null)
            { 
                if(selected!=null)
                    node.setSetting(id, selected.value);
                else
                    node.setSetting(id,"");
                
            }

           

        }

    }
}
